<template>
  <div>
    <div class="filter-container">
      <el-button size="small" type="primary" @click="onAdd">增加礼品</el-button>
    </div>
    <el-table v-loading="loading" :data="giftList" stripe>
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column prop="name" label="礼品名称"></el-table-column>
      <el-table-column label="图片" width="180">
        <template slot-scope="scope">
          <img :src="scope.row.download_url" alt height="60" />
        </template>
      </el-table-column>
      <el-table-column prop="integral" label="所需乐豆"></el-table-column>
      <el-table-column prop="number" label="礼品货储"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="onEdit(scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="onDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 确认删除的对话框 -->
    <el-dialog title="提示" :visible.sync="delDialogVisible" width="30%">
      <span>确定删除该礼品吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doDel">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, del } from "@/api/gift";
import scroll from "@/utils/scroll";
export default {
  data() {
    return {
      giftList: [],
      count: 50,
      loading: false, //加载动画
      delDialogVisible: false, //删除的对话框是否显示
      gift: {},
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    scroll.start(this.getList);
  },
  methods: {
    getList() {
      this.loading = true;
      fetchList({
        start: this.giftList.length,
        count: this.count,
      }).then((res) => {
        console.log(res);
        this.giftList = this.giftList.concat(res.data);
        if (res.data.length < this.count) {
          scroll.end();
        }
        this.loading = false;
      });
    },
    onEdit(row) {
      this.$router.push(`/gift/edit/${row._id}`);
    },
    onAdd() {
      this.$router.push(`/gift/add`);
    },
    onDel(row) {
      this.delDialogVisible = true;
      this.gift = row;
    },
    doDel() {
      this.loading = true;
      this.delDialogVisible = false;
      del(this.gift).then((res) => {
        this.loading = false;
        console.log(res);
        if (res.data.delDBRes.deleted > 0) {
          this.giftList = [];
          this.getList();
          this.$message({
            message: "删除成功",
            type: "success",
          });
        } else {
          this.$message.error("删除失败");
        }
      });
    },
  },
};
</script>

<style>
</style>